package com.demo.nio.ByteBuffer;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class StringBufferConvert {

    public static void main(String[] args) {
        encodeAndDecode();

    }

    public static void encodeAndDecode() {
        //encode part

        //直接put转换
        //buffer1 此时为写模式， capacity = 10
        ByteBuffer buffer1 = ByteBuffer.allocate(10);
        buffer1.put("hello".getBytes());

        //Chartset.encode
        //buffer2 此时已切换为读模式, position = 0, capacity 为入参字节长度
        ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("hello");

        //wrap
        //buffer3 此时已切换为读模式, position = 0, capacity 为入参字节长度
        ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());

        //decode part
        String string = StandardCharsets.UTF_8.decode(buffer2).toString();

    }



}
